package org.neuedu.his.service.sys;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.neuedu.his.mapper.DepartmentMapper;
import org.neuedu.his.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class DepartmentSerivce {
    @Autowired
    DepartmentMapper departmentMapper;
    public List<Department> getDeptList() {
        return departmentMapper.getDeptList();
    }

    public PageInfo serchTableData(String keyWord,int pageNum,int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Department> departments= departmentMapper.serchTableData(keyWord);
        PageInfo dept =new PageInfo(departments);
        return dept;

    }


    public RespBean addDept(AddDept addDept) {
        String deptCode=addDept.getDeptCode();
        String deptName=addDept.getDeptName();
        int constantId =addDept.getConstantId();
        int deptTyptId =addDept.getDeptTyptId();

        int count=departmentMapper.addDept(deptCode,deptName,constantId,deptTyptId);
        if (count==0){
            return RespBean.error(500,"添加失败");
        }else {
            return RespBean.success(200,"添加成功");
        }
    }

    public List<Constantitem> serchconstantList() {
        return departmentMapper.serchconstantList();
    }

    public List<DepartmentType> serchdeptTyptList() {
        return departmentMapper.serchdeptTyptList();

    }

    public Department serchDeptbyId(int deptId) {
        return departmentMapper.serchDeptbyId(deptId);

    }

    public RespBean editDept(EditDept editDept) {
        int id =editDept.getDeptId();
        String deptCode=editDept.getDeptCode();
        String deptName=editDept.getDeptName();
        int constantId =editDept.getConstantId();
        int deptTyptId =editDept.getDeptTyptId();

        int count=departmentMapper.editDept(id,deptCode,deptName,constantId,deptTyptId);
        if (count==0){
            return RespBean.error(500,"更新失败");
        }else {
            return RespBean.success(200,"更新成功");
        }


    }

    public RespBean delDept(int id) {
       int count= departmentMapper.delDept(id);
        if (count==0){
            return RespBean.error(500,"删除失败");
        }else {
            return RespBean.success(200,"删除成功");
        }
    }

    public RespBean mulDelDepts(int[] ids) {
        int count =departmentMapper.mulDelDepts(ids);
        if (count==0){
            return RespBean.error(500,"删除失败");
        }else {
            return RespBean.success(200,"删除成功");
        }

    }
}
